<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      盒子{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="boxForm" ref="boxForm"  label-width="100px">

        <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="盒子id" prop="boxId">
              <el-input type="text" v-model="boxForm.boxId"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="项目id" prop="projectId">
              <el-input type="text" v-model="boxForm.projectId" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="变量数" prop="variantsNum">
           <el-input type="number" v-model="boxForm.variantsNum" clearable></el-input>

            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="boxForm.status" placeholder="请选择状态" style="width: 100%">
                <el-option :value="0" label="离线"></el-option>
                <el-option :value="1" label="在线"></el-option>
                <el-option :value="2" label="活跃报警未处理"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelAdmin">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createBox">创 建</el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateBox">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {addInBox, updateInBox,getInBoxVo,getInBoxList} from "@/api/box";

export default {
  name: "boxForm",

  data() {
    return {
      boxForm: {
        status: 0,
      },

      addLoading:false,
      statusList:[]

    }
  },
  computed: {
    createStatus() {
      return this.$route.params.id == '0'
    },
    headers() {
      return {

      }
    },
  },
  created() {
    if(!this.createStatus) {
      this.getInBoxVo(this.$route.params.id)
    }
  },
  methods: {
    getInBoxVo(val) {
      getInBoxVo({id: val}).then(res => {
        console.log(res.data)
        this.boxForm = res.data

      }).catch(res => {

      })
    },
    cancelAdmin() {
      window.history.back()
    },
    createBox() {
      this.addLoading = true
      this.$refs['boxForm'].validate(valid => {
        if (valid) {
          addInBox(this.boxForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加管理员盒子成功"
            })
            this.cancelAdmin()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },

    updateBox() {
      this.addLoading = true
      this.$refs['boxForm'].validate(valid => {
        if (valid) {
          updateInBox(this.boxForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新盒子成功"
            })
            this.cancelAdmin()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },

  }
}
</script>

<style>
/*.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}*/
</style>